import java.awt.Color;
import java.util.LinkedList;


public class Player {
	Color mycolor;
	static int lastmovesrc=-1;
	static int lastmovedest=-1;
	int position; //6 possible position 
	private int mystone[] = new int[10]; //contain absolute position of these stone
	private  int objective[] ={0,1,47,48,46,2,83,49,45,3}; // objective position 
	Board br;
	/***
	 * 
	 * @param position
	 * @param br
	 * Create an new player on the board
	 */
	public Player(int position,Board br) {
		this.br=br;
		mystone=generateInitStone(position);
		this.position=position;
		mycolor = new Color(((position+1)&1)*255, (((position+1)&2)>>1)*255, (((position+1)&4)>>2)*255);//6 different color
		System.out.println(position+":"+mycolor);
	}
	/***
	 * 
	 * @param i
	 * @return objective of this  position i in relative coordinate
	 */
	public  int getObjective(int i){
		return objective[i];
	}

	/***
	 * 
	 * @param i
	 * @return objective of this  position i in absolute coordinate
	 */
	public  int getObjectiveAbs(int i){
		//return ;
		return Place.rotate_to_absolute(objective[i],br.player.position);
	}
	/***
	 * 
	 * @param i
	 * @return stone position in absolute coordinate
	 */
	public int getStoneAbs(int i){
		return mystone[i];
	}
	/***
	 * 
	 * @param i
	 * @return stone position in relative coordinate
	 */
	public int getStone(int i){
		return Place.rotate_to_position(mystone[i],br.player.position);
	}

	/***
	 * 
	 * @param k
	 * @return true this stone is objective
	 */
	public  boolean inObjective(int k){

		for (int i=0;i<objective.length;i++){
			if (objective[i]==k)return true;
		}

		return false;
	}




	/***
	 * 
	 * @param i
	 * @return true if this stone position is mine
	 */
	boolean isMyStone(int i){
		i=Place.rotate_to_absolute(i,(br.player.position));//done
		for (int k=0;k<mystone.length;k++){
			if(mystone[k]==i)return true;
		}
		return false;
	}


	/***
	 * move the stone from src to dest with absolute position (if possible)
	 * @return true if the move is done
	 */
	public boolean move(int src,int dest){
		// we should check Place consistency

		return move_absolute(Place.rotate_to_absolute(src,br.player.position),Place.rotate_to_absolute(dest,br.player.position));//done
	}
	
	public int CalculDistanceFin(){
		
		int count=0;

		for (int i =0;i<10;i++){
		
			count+=br.min_distance_from(
					Place.rotate_to_position(getStoneAbs(i),position),0);
			
		}
		
		
		return 140-count;
		
		
	}






	private boolean move_absolute(int selectedStone, int i) {// src, dest,places
		lastmovesrc=selectedStone;
		lastmovedest=i;
		//System.out.println("move from: "+selectedStone+" to: "+i);
		for (int k=0;k<mystone.length;k++){
			if((mystone[k]==selectedStone) && (br.getPlace(i).free==-1 ) && (GetPossibilities(Place.rotate_to_position(selectedStone,br.player.position)).contains(Place.rotate_to_position(i,br.player.position)))){ //done
				//if((mystone[k]==selectedStone) && (Board.getPlaceAbs(i).free==-1 ) && (GetPossibilities(selectedStone).contains(i))){	
				//	check if it is possible!
				mystone[k]=i;
				br.getPlace(i).free=i;
				br.getPlace(selectedStone).free=-1;
				return true;
			}
		}			
		//System.out.println(" => forbidden");
		return false;
	}

	/***
	 * NOT TESTED!
	 * @return true if this player win
	 */
	public boolean isWinner(){
		for(int i=0;i<objective.length;i++){
			if(!isMyStone(objective[i]))return false;
		}

		return true;
	}

	/***
	 * @return all stone possibilities of move at the relative position i
	 * <br>
	 * <img src="resources/possibility.jpg" alt="Possibility" />
	 */

	public LinkedList<Integer> GetPossibilities(int i) {
		//System.out.println("check possibility of "+i);
		i=Place.rotate_to_absolute(i,br.player.position);// go in absolute position //done
		LinkedList<Integer>  possible=new LinkedList<Integer>();
		for (int k=0;k<br.getPlace(i).neigbors.length;k++){
			if(br.getPlace(br.getPlace(i).neigbors[k]).free==-1)	{ //near free place
				possible.add(br.getPlace(i).neigbors[k]);

			}else{
				possible=GetPossibilitiesbyjump(br.getPlace(i),br.getPlace(br.getPlace(i).neigbors[k]),possible);

			}
		}

		for(int k=0;k<possible.size();k++){// return in relative position
			possible.set(k, Place.rotate_to_position(possible.get(k),br.player.position));//done

		}

		return possible;
	}
	/*
	 * used for a recursive process of GetPossibility
	 */
	private LinkedList<Integer> GetPossibilitiesbyjump(Place origine,Place jumped,LinkedList<Integer> possible) {


		int jump= findNext(origine, jumped, jumped.bx-origine.bx, jumped.by-origine.by);

		if (jump!=-1 && 	!possible.contains(jump)){
			possible.add(jump);
			for (int k=0;k<br.getPlace(jump).neigbors.length;k++){
				if(br.getPlace(br.getPlace(jump).neigbors[k]).free!=-1)
					possible=GetPossibilitiesbyjump(br.getPlace(jump),br.getPlace(br.getPlace(jump).neigbors[k]), possible) ;
			}

		}

		return possible;
	}
	/*
	 * used by GetPossibilitiesbyjump
	 */
	private int findNext(Place origine,Place pl,int dbx, int dby){
		for(int i=0;i<pl.neigbors.length;i++){
			if(origine.position!=br.getPlace(pl.neigbors[i]).position){

				if((br.getPlace(pl.neigbors[i]).bx==pl.bx+dbx&&br.getPlace(pl.neigbors[i]).by==pl.by+dby)){
					if(br.getPlace(pl.neigbors[i]).free==-1)
						return pl.neigbors[i];
					break;
				}
			}

		}
		return -1;// no possibility	
	}
	private int[] generateInitStone(int position){
		int k=0;
		for(int i=0;i<4;i++){
			mystone[k++]=(i+8*position)%48;
		}
		for(int i=45;i<48;i++){
			mystone[k++]=(i+8*position)%48;
		}

		mystone[k++]=(48+6*position)%84;
		mystone[k++]=(49+6*position)%84;
		mystone[k]=53+((6*((position+5)%6)));


		return mystone;
	}



}
